#include <iostream>
using namespace std;

const int N = 5e3 + 10;

int n,m;
int a[N][N],f[N][N];

int main(){
	cin >> n >> m;
	while(n--){
		int x,y,v;
		cin >> x >> y >> v;
		x++,y++;
		a[x][y] += v;
	}
	int n = 5001;
	for(int i = 1;i <= n;i++){
		for(int j = 1;j <= n;j++){
			f[i][j] = f[i - 1][j] + f[i][j - 1] - f[i - 1][j - 1] + a[i][j];
		}
	}
	int ret = 0;
	m = min(n,m);
	for(int x = m;x <= n;x++){
		for(int y = m;y <= n;y++){
			int x1 = x - m + 1,y1 = y - m + 1;
			int sum = f[x][y] - f[x][y1 - 1] - f[x1 - 1][y] + f[x1 - 1][y1 - 1];
			ret = max(ret,sum);
		}
	}
	cout << ret << endl;
	return 0;
}
